<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class permissions
 * 权限节点管理
 */

class Npermissions extends CT_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->model('npermissions_model');
        $this->t = $this->ct_template;
    }

    /**
     * 权限列表
     *
     */
    public function index()
    {
        $data=$condition = array();
        //获取搜索条件
        $search_name=$this->input->get("name",TRUE)?$this->input->get("name",TRUE):'';
        if(!empty($search_name)){
            $data['name']=$search_name;
            $condition['name  like']="%" . $search_name . "%";
        }
        $search_link=$this->input->get("link",TRUE)?$this->input->get("link",TRUE):'';
        if(!empty($search_link)){
            $data['link']=$search_link;
            $condition['link like']="%" .$search_link. "%";
        }
        $page = $this->input->get('per_page') ? intval($this->input->get('per_page')) : 1;
        $pagesize = $this->config->item('per_page')?$this->config->item('per_page'):20;
        $start = ($page-1) * $pagesize;

        //获取总数
        $this->npermissions_model->findAll( $condition,null,null,null,null);
        $total =$this->npermissions_model->__numRows;
        $permissions_all = $this->npermissions_model->findAll( $condition,null,null,$start,$pagesize);
        $pagelink = $this->pagination($total,$pagesize);
        $data['pagelink'] = $pagelink;
        $data['permissions_all'] = $permissions_all;
        $data['_title'] ='权限列表';
        $this->t->write('_title', '园所权限列表');
        $this->t->write('breadcrum', '园所权限列表');

        $this->t->write_view('content', 'admin/npermissions/index', $data, false);
        $this->t->render();
    }


    /**
     * 权限添加
     */
    function add()
    {
             //表单验证
        $this->lang->load('form_validation', 'chinese');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $data = array( );
        $this->form_validation->set_rules('name', '权限名称', 'required');
        if( $this->form_validation->run() == FALSE ){

        }else{

            $name = $this->input->post('name',TRUE);

            //判断名称中是否有特殊字符
            $pattern="/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_\-]+$/u";
            if(! preg_match($pattern,$name)){
                $alerts = array('error' => '添加失败，名称存在非法字符！');
                $this->common_jump_url($alerts);
            }

            //验证名称是否已经存在
            $check_name_res=$this->npermissions_model->check_permissions_name($name,NULL);
            if(!empty($check_name_res))
            {
                $alerts = array('error' => '添加失败，权限名称已存在');
                $this->common_jump_url($alerts);
            }

            $link=$this->input->post('link',TRUE) ? $this->input->post('link',TRUE):'';

            //验证链接是否已经存在
            if(!empty($link))
            {
                $check_link_res=$this->npermissions_model->check_link($link,NULL);
                if(!empty($check_link_res))
                {
                    $alerts = array('error' => '添加失败，链接已存在');
                    $this->common_jump_url($alerts);
                }

            }


            $pid=$this->input->post('pid',TRUE);
            $data = array(
                    'name'=>$name,
                    'link'=>$link,
                    'pid'=>$pid
            );
            $this->npermissions_model->insert($data);
            $alerts = array('success' => '权限添加成功');
            $this->common_jump_url($alerts,'/admin/npermissions/');



        }
        //获取已经存在的顶级权限列表
        $top_permissions=$this->npermissions_model->findAll(array("pid"=>0));
        $data['top_permissions']=$top_permissions;
        $data['_title'] ='添加权限';
        $this->t->write('_title', '园所权限添加');
        $this->t->write('breadcrum', '园所权限添加');
        $this->t->write_view('content', 'admin/npermissions/add', $data, false);
        $this->t->render();
    }


    /**
     * 权限编辑
     * @param $id
     */
    public  function edit()
    {

        $permissions_id=$this->input->get_post("id",TRUE);
        if(empty($permissions_id)){
            $this->load->helper('url');
            redirect('/admin/npermissions/');
            return;
        }

        //表单验证
        $this->lang->load('form_validation', 'chinese');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', '权限名称', 'required');

        if ( $this->form_validation->run() == FALSE ) {

        }else{
            $name = $this->input->post('name',TRUE);
            $link=$this->input->post('link',TRUE);
            $pid=$this->input->post('pid',TRUE);

            //判断名称中是否有特殊字符
            $pattern="/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_\-]+$/u";
            if(! preg_match($pattern,$name))
            {
                $alerts = array('error' => '修改失败，名称存在非法字符！');
                $this->common_jump_url($alerts);
            }
            //验证权限名称是否已经存在
            $check_res=$this->npermissions_model->check_permissions_name($name,$permissions_id);
            if(!empty($check_res))
            {
                $alerts = array('error' => '编辑失败，权限名称已存在');
                $this->common_jump_url($alerts);
            }

            $field = array(
                    'name'=>$name,
                    'link'=>$link,
                    'pid'=>$pid
            );
            $where = array('id'=>$permissions_id);
            $this->npermissions_model->update($field,$where);
            $alerts = array('success' => '权限编辑成功');
            $this->session->set_flashdata('alerts', $alerts);
            $this->load->helper('url');
            redirect('/admin/npermissions/');
            return;


        }

        $data = array( );
        //获取已经存在的顶级权限列表
        $top_permissions=$this->npermissions_model->findAll(array("pid"=>0));
        $data['top_permissions']=$top_permissions;
        $row = $this->npermissions_model->find( array('id'=>$permissions_id));
        $data['permissions_id']=$permissions_id;
        $data['name'] = $row['name'];
        $data['link']=$row['link'];
        $data['pid']=$row['pid'];
        $data['_title'] ='编辑权限';
        $this->t->write('_title', '园所权限编辑');
        $this->t->write('breadcrum', '园所权限编辑');
        $this->t->write_view('content', 'admin/npermissions/edit', $data, false);
        $this->t->render();
    }



    /**
     * 权限删除
     */

    function permissions_del()
    {

        $permissions_id=$this->input->get("id",TRUE);
        if(!empty($permissions_id)){
            $where = array('id'=>$permissions_id);
            $this->npermissions_model->delete($where);
            $alerts = array('success' => '权限删除成功');
            $this->common_jump_url($alerts);
        }else{
            $alerts = array('error' => '参数有误，权限删除失败');
            $this->common_jump_url($alerts,'/admin/npermissions/');
        }

    }

}
